package cn.iocoder.yudao.module.wine.service.complaintype;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.admin.complaintype.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.complaintype.ComplainTypeDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.complaintype.ComplainTypeMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 投诉信息类型 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ComplainTypeServiceImpl implements ComplainTypeService {

    @Resource
    private ComplainTypeMapper complainTypeMapper;

    @Override
    public Integer createComplainType(ComplainTypeSaveReqVO createReqVO) {
        // 插入
        ComplainTypeDO complainType = BeanUtils.toBean(createReqVO, ComplainTypeDO.class);
        complainTypeMapper.insert(complainType);
        // 返回
        return complainType.getId();
    }

    @Override
    public void updateComplainType(ComplainTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateComplainTypeExists(updateReqVO.getId());
        // 更新
        ComplainTypeDO updateObj = BeanUtils.toBean(updateReqVO, ComplainTypeDO.class);
        complainTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteComplainType(Integer id) {
        // 校验存在
        validateComplainTypeExists(id);
        // 删除
        complainTypeMapper.deleteById(id);
    }

    private void validateComplainTypeExists(Integer id) {
        if (complainTypeMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public ComplainTypeDO getComplainType(Integer id) {
        return complainTypeMapper.selectById(id);
    }

    @Override
    public PageResult<ComplainTypeDO> getComplainTypePage(ComplainTypePageReqVO pageReqVO) {
        return complainTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ComplainTypeDO> selectTypeList() {
        return complainTypeMapper.selectList(new LambdaQueryWrapper<ComplainTypeDO>()
                // 查询可见的类型信息
                .eq(ComplainTypeDO::getVisible,1)
                .orderByAsc(ComplainTypeDO::getSort));

    }

}
